﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TEG.Models;

namespace TEG.Controllers
{ 
    public class HistorialFallaVehiculosController : Controller
    {
        private ArlandriaEntities db = new ArlandriaEntities();

        //
        // GET: /HistorialFallaVehiculos/

        public ViewResult Index()
        {
            var historialfallavehiculo = db.HistorialFallaVehiculo.Include("Falla").Include("Vehiculo");
            return View(historialfallavehiculo.ToList());
        }

        //
        // GET: /HistorialFallaVehiculos/Details/5

        public ViewResult Details(int id)
        {
            HistorialFallaVehiculo historialfallavehiculo = db.HistorialFallaVehiculo.Single(h => h.Id == id);
            return View(historialfallavehiculo);
        }

        //
        // GET: /HistorialFallaVehiculos/Create

        public ActionResult Create()
        {
            ViewBag.IdFalla = new SelectList(db.Falla, "Id", "Codigo");
            ViewBag.IdVehiculo = new SelectList(db.Vehiculo, "Id", "Placa");
            return View();
        } 

        //
        // POST: /HistorialFallaVehiculos/Create

        [HttpPost]
        public ActionResult Create(HistorialFallaVehiculo historialfallavehiculo)
        {
            if (ModelState.IsValid)
            {
                db.HistorialFallaVehiculo.AddObject(historialfallavehiculo);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.IdFalla = new SelectList(db.Falla, "Id", "Codigo", historialfallavehiculo.IdFalla);
            ViewBag.IdVehiculo = new SelectList(db.Vehiculo, "Id", "Placa", historialfallavehiculo.IdVehiculo);
            return View(historialfallavehiculo);
        }
        
        //
        // GET: /HistorialFallaVehiculos/Edit/5
 
        public ActionResult Edit(int id)
        {
            HistorialFallaVehiculo historialfallavehiculo = db.HistorialFallaVehiculo.Single(h => h.Id == id);
            ViewBag.IdFalla = new SelectList(db.Falla, "Id", "Codigo", historialfallavehiculo.IdFalla);
            ViewBag.IdVehiculo = new SelectList(db.Vehiculo, "Id", "Placa", historialfallavehiculo.IdVehiculo);
            return View(historialfallavehiculo);
        }

        //
        // POST: /HistorialFallaVehiculos/Edit/5

        [HttpPost]
        public ActionResult Edit(HistorialFallaVehiculo historialfallavehiculo)
        {
            if (ModelState.IsValid)
            {
                db.HistorialFallaVehiculo.Attach(historialfallavehiculo);
                db.ObjectStateManager.ChangeObjectState(historialfallavehiculo, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.IdFalla = new SelectList(db.Falla, "Id", "Codigo", historialfallavehiculo.IdFalla);
            ViewBag.IdVehiculo = new SelectList(db.Vehiculo, "Id", "Placa", historialfallavehiculo.IdVehiculo);
            return View(historialfallavehiculo);
        }

        //
        // GET: /HistorialFallaVehiculos/Delete/5
 
        public ActionResult Delete(int id)
        {
            HistorialFallaVehiculo historialfallavehiculo = db.HistorialFallaVehiculo.Single(h => h.Id == id);
            return View(historialfallavehiculo);
        }

        //
        // POST: /HistorialFallaVehiculos/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            HistorialFallaVehiculo historialfallavehiculo = db.HistorialFallaVehiculo.Single(h => h.Id == id);
            db.HistorialFallaVehiculo.DeleteObject(historialfallavehiculo);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}